package utils;

import javax.ejb.Local;
import javax.ejb.Stateless;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class EJBFactory {

	private final static String EARFILE_PREFIX = "T5EARProj/";
	
	public static <T> T createSessionBean(Class<T> beanInterface, String mName) {
		
		
		String jndiName = null;
		// String jndiName = "java:comp/env/TestBean";
		
		if (beanInterface.getAnnotation(Local.class) != null){
			jndiName = getLocalJNDIName(beanInterface,mName);
		}
		
		if (jndiName == null)
			throw new RuntimeException("The bean interface should have been marked with @Local annotation.");
		
		try {
			Context ctx  = new InitialContext();

			return (T)ctx.lookup(jndiName);
		} catch (NamingException e) {
			throw new RuntimeException(e);
		}
	}
	
	public static String getLocalJNDIName(Class<?> beanInterface, String mName){
		//return "java:comp/env/UserManager";
		return EARFILE_PREFIX + mName + "/local";
	}
}

